<?php

namespace app\admin\controller\scanwork;

use app\common\controller\Backend;
use app\admin\model\scanwork\Config as ConfigModel;

/**
 * scanwork配置管理
 */
class Config extends Backend
{
    protected $model = null;
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new ConfigModel;
    }

    /**
     * 配置页面
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $configs = ConfigModel::getAllConfigs();
            
            $result = [
                'code' => 1,
                'msg' => '获取成功',
                'data' => $configs
            ];
            
            return json($result);
        }
        
        return $this->view->fetch();
    }

    /**
     * 获取配置数据
     */
    public function get()
    {
        $configs = ConfigModel::getAllConfigs();
        
        $result = [
            'code' => 1,
            'msg' => '获取成功',
            'data' => $configs
        ];
        
        return json($result);
    }

    /**
     * 保存配置
     */
    public function save()
    {
        if (!$this->request->isPost()) {
            $this->error('请求方式错误');
        }
        
        $data = $this->request->post();
        
        if (empty($data)) {
            $this->error('参数不能为空');
        }
        
        $result = ConfigModel::setConfigs($data);
        
        if ($result) {
            $this->success('保存成功');
        } else {
            $this->error('保存失败');
        }
    }

    /**
     * 测试配置
     */
    public function test()
    {
        if (!$this->request->isPost()) {
            $this->error('请求方式错误');
        }
        
        $type = $this->request->post('type');
        $result = ['success' => false, 'message' => ''];
        
        switch ($type) {
            case 'domain':
                $domain = ConfigModel::getSiteDomain();
                $result = [
                    'success' => !empty($domain),
                    'message' => !empty($domain) ? "域名配置正常: {$domain}" : '域名配置为空'
                ];
                break;
                
            case 'qiniu':
                $accessKey = ConfigModel::getValue('qiniu_access_key');
                $secretKey = ConfigModel::getValue('qiniu_secret_key');
                $bucket = ConfigModel::getValue('qiniu_bucket');
                $domain = ConfigModel::getValue('qiniu_domain');
                
                if (empty($accessKey) || empty($secretKey) || empty($bucket) || empty($domain)) {
                    $result = [
                        'success' => false,
                        'message' => '七牛云配置不完整，请检查AccessKey、SecretKey、Bucket和域名配置'
                    ];
                } else {
                    $result = [
                        'success' => true,
                        'message' => '七牛云配置完整'
                    ];
                }
                break;
                
            case 'miniapp':
                $appid = ConfigModel::getValue('miniapp_appid');
                $secret = ConfigModel::getValue('miniapp_secret');
                
                if (empty($appid) || empty($secret)) {
                    $result = [
                        'success' => false,
                        'message' => '小程序配置不完整，请检查AppID和AppSecret'
                    ];
                } else {
                    $result = [
                        'success' => true,
                        'message' => '小程序配置完整'
                    ];
                }
                break;
                
            default:
                $result = [
                    'success' => false,
                    'message' => '未知的测试类型'
                ];
        }
        
        if ($result['success']) {
            $this->success($result['message']);
        } else {
            $this->error($result['message']);
        }
    }
}
